import { http } from "@/utils/http";
import { formatToken } from "@/utils/auth";
import type { ApiResponse, IdentityInfo } from "./user";

export interface CompanyInvoiceInfo {
  invoice_title: string;
  invoice_taxno: string;
  bank_name?: string;
  bank_account?: string;
  bank_account_name?: string;
  contact_name?: string;
  contact_phone?: string;
  contact_email?: string;
}

export interface CompanyAddressInfo {
  address_type: "shipping" | "billing";
  contact_name: string;
  contact_phone: string;
  province: string;
  city: string;
  district: string;
  address: string;
  postal_code?: string;
  is_default?: number;
}

export interface CompanyVerificationInfo {
  company_name: string;
  license_code: string;
  license_photo?: string;
  legal_rep?: string;
  address?: string;
  scope?: string;
  region_id?: number | null;
  invoice?: CompanyInvoiceInfo | null;
  addresses?: CompanyAddressInfo[];
  raw?: Record<string, any> | null;
  status?: number;
  verified_time?: string | null;
}

export interface CompanyVerificationResponse {
  company: CompanyVerificationInfo | null;
  identities: Array<IdentityInfo>;
}

export const fetchCompanyVerification = (partialToken: string) => {
  return http.request<ApiResponse<CompanyVerificationResponse>>(
    "get",
    "/auth/verification/company",
    {},
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};

export const saveCompanyVerification = (
  partialToken: string,
  payload: CompanyVerificationInfo
) => {
  return http.request<ApiResponse<CompanyVerificationResponse>>(
    "post",
    "/auth/verification/company",
    {
      data: payload
    },
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};

export const saveCompanyInvoice = (
  partialToken: string,
  payload: CompanyInvoiceInfo
) => {
  return http.request<ApiResponse<CompanyVerificationResponse>>(
    "post",
    "/auth/verification/invoice",
    {
      data: payload
    },
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};

export const saveCompanyAddresses = (
  partialToken: string,
  addresses: CompanyAddressInfo[]
) => {
  return http.request<ApiResponse<CompanyVerificationResponse>>(
    "post",
    "/auth/verification/addresses",
    {
      data: { addresses }
    },
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};

export const recognizeBusinessLicense = (
  partialToken: string,
  payload: { image_url: string }
) => {
  return http.request<
    ApiResponse<CompanyVerificationInfo & { raw?: Record<string, any> }>
  >(
    "post",
    "/auth/verification/license-ocr",
    {
      data: payload
    },
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};

export interface RegionTreeNode {
  id: string | number;
  name: string;
  level: number;
  code?: string;
  children?: RegionTreeNode[];
}

export const fetchVerificationRegions = (partialToken: string) => {
  return http.request<ApiResponse<RegionTreeNode[]>>(
    "get",
    "/auth/verification/regions",
    {},
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};

export const fetchGeoRegions = (partialToken: string) => {
  return http.request<ApiResponse<RegionTreeNode[]>>(
    "get",
    "/auth/verification/geo-regions",
    {},
    {
      headers: {
        Authorization: formatToken(partialToken)
      }
    }
  );
};
